package top.chukongxiang.monitorservice.config;

import javafx.beans.value.ChangeListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/24 09:43
 */
public class ApplicationStatusContextHolder {

    private static volatile ApplicationState applicationState = ApplicationState.STOPPED;
    private static List<ChangeListener<ApplicationState>> eventListeners = new ArrayList<>();

    public static ApplicationState getCurrentStatus() {
        return applicationState;
    }

    public static void setState(ApplicationState applicationState) {
        for (ChangeListener<ApplicationState> eventListener : eventListeners) {
            eventListener.changed(null, ApplicationStatusContextHolder.applicationState, applicationState);
        }
        ApplicationStatusContextHolder.applicationState = applicationState;
    }

    public static void registerListener(ChangeListener<ApplicationState> changeListener) {
        eventListeners.add(changeListener);
    }
}
